import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart' hide Action;
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/home_page/subject_tab_component/item/state.dart';
import 'package:www.znpigai.teacher/utils/http.dart';
import 'package:www.znpigai.teacher/utils/nav.dart';
import 'package:www.znpigai.teacher/utils/progress_dialog.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Effect<SubjectEditState> buildEffect() {
  return combineEffects(<Object, Effect<SubjectEditState>>{
    SubjectEditAction.onDone: _onDone,
    SubjectEditAction.onSubject: _onSubject,
    SubjectEditAction.onEdit: _onEdit,
  });
}

void _onSubject(Action action, Context<SubjectEditState> ctx) async {
  var subject = await Navigator.of(ctx.context).pushNamed(Nav.SUBJECT_LIST);
  print(subject);
  if (subject is SubjectState) {
    ctx.dispatch(SubjectEditActionCreator.edit({
      'title': subject.title,
      'demand': subject.demand,
      'keywords': subject.keywordList.join(' ') ?? '',
      'ideation': subject.ideation ?? ''
    }));
  }
}

void _onEdit(Action action, Context<SubjectEditState> ctx) async {
  var inputType = action.payload;
  var ret;
  switch (inputType) {
    case 'title':
      ret = await Util.showInputDialog(
          '请输入标题', '', ctx.state.subject.title, '请输入标题', ctx.context);
      break;
    case 'demand':
      ret = await Util.showInputDialog(
          '请输入作文要求', '', ctx.state.subject.demand, '请输入作文要求', ctx.context);
      break;
    case 'ideation':
      ret = await Util.showInputDialog(
          '请输入构思点评', '', ctx.state.subject.ideation, '请输入构思点评', ctx.context);
      break;
    case 'keywords':
      ret = await Util.showInputDialog('请输入中心词', '',
          ctx.state.subject.keywordList.join(' '), '请输入中心词', ctx.context);
      break;
    case 'type':
      ret = await Util.showMultiSelectDialog(
          '请选择常考题型',
          GlobalState.articleTypes,
          ctx.state.subject.articleTypeList,
          ctx.context);
      break;
    case 'grade':
      ret = await Util.showMultiSelectDialog('请选择年级', GlobalState.grades,
          ctx.state.subject.gradeList, ctx.context);
      break;
    case 'tagList':
      ret = await Util.showMultiSelectDialog(
          '请选择常考题型', GlobalState.tags, ctx.state.subject.tagList, ctx.context);
      break;
    case 'themeList':
      ret = await Util.showMultiSelectDialog('请选择作文主题', GlobalState.themes,
          ctx.state.subject.themeList, ctx.context);
      break;
    default:
      throw UnimplementedError();
  }

  if (ret != null) {
    ctx.dispatch(SubjectEditActionCreator.edit({inputType: ret}));
  }
}

Future _onDone(Action action, Context<SubjectEditState> ctx) async {
  if (ctx.state.subject.title.isEmpty) {
    Util.alert('标题不能为空', ctx.context);
    return;
  }

  if (ctx.state.subject.demand.isEmpty) {
    Util.alert('写作要求不能为空', ctx.context);
    return;
  }

  var pr = new ProgressDialog(ctx.context)..setMessage('请等待...');
  pr.show();

  HttpResult result = await Http.post(
      '/correct/teacher/api/homework/entrepot/save.mvc',
      isJson: true,
      data: {
        'id': ctx.state.subject.id,
        'gradeList': ctx.state.subject.gradeList,
        'themeList': ctx.state.subject.themeList,
        'typeList': ctx.state.subject.typeList,
        'articleTypeList': ctx.state.subject.articleTypeList,
        'tagList': ctx.state.subject.tagList,
        'keywordList': ctx.state.subject.keywordList,
        'title': ctx.state.subject.title,
        'demand': ctx.state.subject.demand,
        'ideation': ctx.state.subject.ideation,
      });
  pr.hide();

  if (result.success) {
    Util.alert('保存成功', ctx.context);
    Navigator.of(ctx.context).pop(true);
  } else {
    Util.alertFail(result, ctx.context);
  }
}
